/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceCartRemoteE;
import com.paris5.master2.guitarmaster.service.ServiceCustomerRemote;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leticia
 */
public class ValiderCommande extends EcomAction {
    
    @Override
    public String execute()
    {
        ServiceCartRemoteE serviceCartRemoteE = (ServiceCartRemoteE) getFromSession("PANIER");
        EcomUser ecomUser = (EcomUser) getFromSession("USER");
        if (ecomUser == null)
        {
            return LOGIN;
        }
        try {
            serviceCartRemoteE.validate(ecomUser.getId());
            ServiceCustomerRemote serviceCustomerRemote = lookupServiceCustomerRemote();
            ecomUser = serviceCustomerRemote.getUser(ecomUser.getId());
            saveSession("USER", ecomUser);
            removeSession("PANIER");
            removeSession("TOTAL_PRICE");
            saveSession("TOTAL_PRICE", new Float(0));
            setMessage("Votre commande vient d'être effectuée.");
            return SUCCESS;
        } catch (EcomValidationException ex) {
            setMessage("Montant insuffisant! vous ne pouvez régler la facture.");
        } catch (EcomException ex) {
            setMessage("Montant insuffisant! vous ne pouvez régler la facture.");
        }
        return ERROR;
    }
}
